package main

import (
	"fmt"
	"go_code/20211014/model"
	"go_code/20211014/service"
)

type customerView struct {
	key             string
	loop            bool
	customerService *service.CustomerService
}

func main() {
	customerView := customerView{
		key:  "",
		loop: true,
	}
	customerView.customerService = service.NewCustomerService()
	customerView.mainMenu()
}

// 显示主菜单
func (this *customerView) mainMenu() {
	for {
		fmt.Println("--------------客户信息管理软件---------------")
		fmt.Println("                           1. 添加客户")
		fmt.Println("                           2. 修改客户")
		fmt.Println("                           3. 删除客户")
		fmt.Println("                           4. 客户列表")
		fmt.Println("                           5. 退   出")
		fmt.Println("请选择（1-5）：")

		fmt.Scanln(&this.key)

		switch this.key {
		case "1":
			fmt.Println()
			fmt.Println("添加客户开始")
			this.add()
			fmt.Println("添加客户结束")
			fmt.Println()
		case "2":
			fmt.Println("修改客户")
		case "3":
			fmt.Println("删除客户")
		case "4":
			fmt.Println()
			fmt.Println("----客户列表开始----")
			this.list()
			fmt.Println("----客户列表结束----")
			fmt.Println()
		case "5":
			this.loop = false
		default:
			fmt.Println("您的输入有误，请重新输入……")
		}

		if !this.loop {
			break
		}
	}
	fmt.Println("你退出了客户管理系统……")
}

func (this *customerView) list() {
	customers := this.customerService.List()
	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t邮箱")
	for i := 0; i < len(customers); i++ {
		fmt.Println(customers[i].ShowCustomer())
	}
}

func (this *customerView) add() {
	fmt.Println("请输入客户姓名：")
	name := ""
	fmt.Scanln(&name)
	fmt.Println("请输入客户性别：")
	gender := ""
	fmt.Scanln(&gender)
	fmt.Println("请输入客户年龄：")
	age := 0
	fmt.Scanln(&age)
	fmt.Println("请输入客户电话：")
	phone := ""
	fmt.Scanln(&phone)
	fmt.Println("请输入客户邮件：")
	email := ""
	fmt.Scanln(&email)
	customer := model.NewCustomerTwo(name, gender, age, phone, email)
	if this.customerService.Add(customer) {
		fmt.Println("添加成功")
	} else {
		fmt.Println("添加失败")
	}
}
